package com.rocching.easychat.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.rocching.easychat.R;
import com.rocching.easychat.core.ViewClickListener;
import com.rocching.easychat.model.FriendModel;
import com.rocching.easychat.model.GroupFriendModel;
import com.rocching.easychat.util.AppUtil;
import com.rocching.easychat.util.StringUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.logging.LogRecord;

public class FriendListExpandableListAdapter extends BaseExpandableListAdapter {
    private final Context context;
    private List<GroupFriendModel> groupList;
    private OnItemClickListener clickListener;

    public FriendListExpandableListAdapter(Context context, List<GroupFriendModel> list) {
        if (Objects.isNull(list)) {
            list = new LinkedList<>();
        }
        this.context = context;
        this.groupList = list;
    }

    public void update(List<GroupFriendModel> list) {
        this.groupList = list;
        notifyDataSetChanged();
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        GroupFriendModel friendModel = groupList.get(i);
        if (!Objects.isNull(friendModel)) {
            List<FriendModel> items = friendModel.getItems();
            if (StringUtil.isNotEmpty(items)) {
                return items.size();
            }
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return groupList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        GroupFriendModel friendModel = groupList.get(i);
        if (!Objects.isNull(friendModel)) {
            List<FriendModel> items = friendModel.getItems();
            if (StringUtil.isNotEmpty(items)) {
                return items.get(i1);
            }
        }
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        FriendListGroupHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_friend_list_group, null);
            holder = new FriendListGroupHolder(view);
            view.setTag(holder);
        } else {
            holder = (FriendListGroupHolder) view.getTag();
        }

        GroupFriendModel friendModel = groupList.get(i);
        if (!Objects.isNull(friendModel)) {
            holder.update(friendModel.getCode());
        }
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        FriendListGroupItemHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_friend_list_group_item, null);
            holder = new FriendListGroupItemHolder(view);
            view.setTag(holder);
        } else {
            holder = (FriendListGroupItemHolder) view.getTag();
        }

        FriendModel friendModel = (FriendModel) getChild(i, i1);
        if (!Objects.isNull(friendModel)) {
            holder.update(friendModel.getFriendName(), friendModel.getPhoto());

            LinearLayout linearLayout = view.findViewById(R.id.friend_item_layout);
            linearLayout.setOnClickListener(new ViewClickListener() {
                @Override
                public void onDClick(View v) {
                    if (clickListener != null) {
                        clickListener.onClick(friendModel);
                    }
                }
            });
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    static class FriendListGroupHolder {
        private final TextView textView;

        public FriendListGroupHolder(View view) {
            textView = view.findViewById(R.id.group_name);
        }

        public void update(String code) {
            textView.setText(code);
        }
    }

    static class FriendListGroupItemHolder {
        private final View view;
        private final TextView textView;
        private final ImageView imageView;

        public FriendListGroupItemHolder(View v) {
            view = v;
            textView = v.findViewById(R.id.friend_name);
            imageView = v.findViewById(R.id.friend_photo);
        }

        public void update(String name, String photo) {
            textView.setText(name);
            Uri uri = AppUtil.getUserPhotoUri(photo);
            if (uri != null) {
                Glide.with(view).load(uri).placeholder(R.drawable.loading).into(imageView);
            }
        }
    }

    public interface OnItemClickListener {
        void onClick(FriendModel model);
    }
}
